<%-- 
    Document   : ViewFile
    Created on : Oct 26, 2014, 9:18:11 AM
    Author     : user
--%>

<%@page import="newpackage.SendEmailKey"%>
<%@page import="java.io.FileNotFoundException"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="newpackage.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>View File Server</title>
        <link rel="stylesheet" type="text/css" href="./jquery.datetimepicker.css"/>
<style type="text/css">

.custom-date-style {
	background-color: red !important;
}

</style>
    
        <script type="text/javascript">
            
            function sendEmail(name)
            {
            document.getElementById("file_name").value=name;
            if(name=='select file')
                {
                    alert('please select file name');
                }
                else
                    {
                        
             document.getElementById("mailflag1").value='true';
         document.forms["view_file"].submit();
                    }
            }
            function selectedfilesubmit(name)
            {
               
                document.view_file.action="./SelectedFile.jsp";
                document.view_file.submit();
            }
            
            function myFunction(name) {
                
 
               
    var key = window.prompt("Please enter Secret Key", "Please enter Secret Key");
    if (key !== null || key!=='Please enter Secret Key')
    {
        viewFile(name,key)
       
    }
               }

            function viewFile(name,key)
            {
               
                document.getElementById("key").value=key;
                document.getElementById("file_name").value=name;
                displayTable(name,key);
                
            }
            
            function displayTable(name,key)
            {
             document.view_file.submit();
             document.getElementById("key").value=key;
            }
            
            
            function alertmsg()
            {
                alert("File Not Found");
            }
            
             function submitForm()
            {
               
               var selectBox = document.getElementById("file_name");
               var selectedValue = selectBox.options[selectBox.selectedIndex].value;
               window.location.assign('SaveFile.jsp?file_name='+selectedValue);
            }
        function serverChange()
            {
             var file=document.getElementById('file_name');
              // alert(file); 
               if(file=='select file')     
                   {
                      alert('correct'); 
                   }
            }
          function submitToRead()
          {
               document.view_file.action="./ReadFile.jsp";
                document.view_file.submit();
          }

        </script>
    </head>
    <body background="image.jpg">
          <form id="view_file" name="view_file" >
              <input type='hidden' name='mailflag' value='false'/>
        <h1> ENSURING PRIVACY AND DATA FRESHNESS OF SHARED DATA IN CLOUD </h1>
        <%
       String key= request.getParameter("key");
       String file_name=request.getParameter("file_name");
       out.println("<input type='hidden' name='key' id='key' value='"+key+"'>");    
       
       if(key!=null&&file_name!=null)
        {
               String short_path=file_name.substring(file_name.lastIndexOf('/') + 1);
           String query1="Select * from file_details where secret_key='"+key+"' and filename like '%"+short_path+"%'";
         Connection con=ConnectionFactory.getConnection();
         Statement st1=con.createStatement();
         ResultSet rs1=st1.executeQuery(query1); 
         
         if(rs1.next())
                         
                       {
           
     
         String query="Select * from update_values where name like '%"+short_path+"%'";
        // Connection con=ConnectionFactory.getConnection();
         Statement st=con.createStatement();
         ResultSet rs=st.executeQuery(query);
     //    out.println("<form name='select_file' action='SelectedFile.jsp'>");
         out.println("<table border='1' style='width:100%'>");    
         out.println("<th>Version</th>");    
         out.println("<th>Server</th>");
         out.println("<th>Time Stamp</th>");
       //  out.println("<th>Select File</th>");
    
         
         
         while(rs.next())
         {
           
           //ReadServer
         out.println("<tr>");
         out.println("<td>"+rs.getString("version") +"</td>");
         out.println("<td>"+rs.getString("readSite") +"</td>");
         out.println("<td>"+rs.getString("time_stamp") +"</td>");
         //out.println("<td><input type='radio' name='selected_file' value='"+file_name.replace("main", "read") +"'></td>");
         out.println("</tr>");
           }
         out.println("</table>");   
         
          out.println("<p align='center'>Time Stamp 1: <input type='text' value='' id='datetimepicker' name='timestamp1'/></p>");
          
          out.println("<p align='center'>Time Stamp 2:<input type='text' value='' id='datetimepicker1' name='timestamp2'/></p>");
          
         out.println("<p align='center'><input type='button' value='Submit'  align='center' onclick='submitToRead()'/></p>");
                
//out.println("<input type='text' value='' id='datetimepicker'/>");
                 }
            else
                     {
           out.println("<h1>Security key is Wrong!!!!!!</h1>");
       }
        }
    
        
        %>
        <center>

        <%!ArrayList<String> getFileNames(String user)throws Exception
         {
         ArrayList<String>  names=new ArrayList<String>();
           Connection con=ConnectionFactory.getConnection();
           Statement select=con.createStatement();
           String Query="SELECT file_details.filename FROM file_details INNER JOIN shared ON shared.id_fk=file_details.id WHERE shared.user_name='"+user+"' ";
           ResultSet rs=select.executeQuery(Query);
           int i=0;
           while(rs.next())
           {
               String s=rs.getString(1);
               names.add(s);
           }
           return names;
        }
        %>
 
        
        <%
     String user_name=(String)session.getAttribute("user_name");
     ArrayList<String> files=getFileNames(user_name);
        %>
        <p>
            Select File Name: <div id="msg" align="center"></div>
        </p>
        
        <select name="file_name"  id="file_name">
     
        <option value="select file" >select file</option>
          <%
          
       if(request.getParameter("file_name")!=null)
       {          
          %>
          <option value="<%=request.getParameter("file_name")%>" selected="" onclick="myFunction(this.value);" ><%=request.getParameter("file_name").substring(request.getParameter("file_name").lastIndexOf('/')+1) %></option>
      <%
      } 
       for(String name:files)
        {
           if(request.getParameter("file_name")!=null)
                             {
       if(request.getParameter("file_name").equals(name))
        continue;     
           }               
         %>
         <option value="<%=name%>" onclick="myFunction(this.value);"><%=name.substring(name.lastIndexOf('/') + 1)%></option>
        <%
        }
        %>
        </select>
        <input type="hidden" name="key" id="key"/>
          <input type="hidden" name="mailflag1" id="mailflag1"/>
  
<!--        <input type="button" value="Save" onclick="submitForm()"/>-->

<p/>



  
<p/>

<a href="Login.jsp">LogOut</a>


 <%!void sendMail(String fn, String un)throws Exception
                               {
           Connection con=ConnectionFactory.getConnection();
           Statement select=con.createStatement();
           String Query="SELECT file_details.secret_key,shared.user_name FROM file_details inner join shared on shared.id_fk=file_details.id where file_details.filename='"+fn+"' and user_name='"+un+"'";
           ResultSet rs=select.executeQuery(Query);
           String s1="";
           String s2="";
           while(rs.next())
                           {
         //  SendEmailKey.sendMail(rs.getString(2),rs.getString(1));
             s1=rs.getString(2);
             s2=rs.getString(1);
              
          }
           SendEmailKey.sendMail(s1,s2);
        }
        
%>
 <%
 String fname=request.getParameter("file_name");
  String u_name=(String)session.getAttribute("user_name");
   String flag=request.getParameter("mailflag1");
 if(fname!=null&&u_name!=null&&(flag.equals("true")))
    {
sendMail(request.getParameter("file_name"),(String)session.getAttribute("user_name"));
 }
 %>
 <p/>
       <%
        String server_name=request.getParameter("server_name");
        if(server_name!=null)
                       {
         if(server_name.equals("read"))
                                     {
          %>
             <script type="text/javascript">
                 alert('Loaded document from read server');
                document.getElementById('msg').innerHTML='Document is read from Read Server';
                document.getElementById("file_content").readOnly = true;
                
              </script>
            
            
            <%
                       }
               }
        %>
 Send Email for selected file <input type="checkbox" value="mailflag" name="mailflag" onclick="sendEmail(file_name.value);"/>
      
          </form>
 
        </center>
    </body>
        <script src='./jquery.js'></script>
<script src='./jquery.datetimepicker.js'></script>
<script>/*
window.onerror = function(errorMsg) {
	$('#console').html($('#console').html()+'<br>'+errorMsg)
}*/
$('#datetimepicker').datetimepicker({
dayOfWeekStart : 1,
lang:'en',
disabledDates:['1986/01/08','1986/01/09','1986/01/10'],
startDate:	'2015/01/05'
});
$('#datetimepicker').datetimepicker({value:'2015/04/15 05:03',step:10});

$('#datetimepicker1').datetimepicker({
dayOfWeekStart : 1,
lang:'en',
disabledDates:['1986/01/08','1986/01/09','1986/01/10'],
startDate:	'2015/01/05'
});
$('#datetimepicker1').datetimepicker({value:'2015/04/15 05:03',step:10});

</script>
</html>
